package org.zjx.service.impl;

import org.zjx.algorithm.DijkstraPathFinding;
import org.zjx.algorithm.PathFindingStrategy;
import org.zjx.core.RouteManager;
import org.zjx.factory.RouteFactory;
import org.zjx.factory.RouteFactoryImpl;
import org.zjx.model.route.Route;
import org.zjx.pojo.RouteQueryPojo;
import org.zjx.service.RouteService;

import java.util.List;

public class RouteServiceImpl implements RouteService {
    private static final RouteManager routeManager;

    static {
        routeManager = createManager();
    }

    @Override
    public List<Route> findPath(RouteQueryPojo routeQueryPojo) {
        return routeManager.findPath(routeQueryPojo);
    }

    @Override
    public Route findPathByOne(RouteQueryPojo routeQueryPojo) {
        List<Route> routes = findPath(routeQueryPojo);
        return !routes.isEmpty() ? routes.get(0) : null;
    }

    @Override
    public void clearPlayerCache(String playerId) {
        routeManager.getCacheManager().cleanPlayerCache(playerId);
    }


    private static RouteManager createManager() {
        RouteFactory routeFactory = new RouteFactoryImpl();
        PathFindingStrategy pathFinder = new DijkstraPathFinding();
        return new RouteManager(pathFinder, routeFactory);
    }
}
